use dataset,clear
ssc inst rowranks, replace
set more off
destring D*, replace force

//recode socio-demographics
//age
drop age
gen age = 2014-birth
recode age (17/29 = 1) (30/44 =2) (45/64=3) (65/100 =4), gen(agefour)
//gender
encode gender, gen (sex)
//income
encode income,gen(n_income)
recode n_i  (27 28 40 47 48 49 =0 "NA") (41 1 44 65 45 70 43 38 29 50 61 51 53 42 18 = 1 "Less than 20") (6 9 11 73 74 75 31 32 33 34 35 55 56 57 58 59 60 19 20 21= 2 "20-50") (12 14 15 16 76 77 78 79 80 81 36 37 39 62 63 64 22 23 24 25 26 = 3 "50-100") (2 3 4 5 7 8 10 13 46 66 67 68 69 71 72 30 52 54 17 = 4 "More than 100"), gen(income4)  
recode n_i (27 28 40 47 48 49 =.) (41 44 45 43 50 61 42 = 1) (1 65 70 38 51 18 =2) ( 6 73 29 53 19 =3) (9 74 31 55  20 =4) (11 75 32 56 21 =5) (12 76 33 57 58 =6) (14 77 34 59 60 22=7) (15 78 35 62 23 =8) (16 79 80 81 36 63 24 =9) (2 66 67 37 64 25=10) (3 68 69 39 52 26=11) (4 5 7 8 10 13 46 72 71 30 54 17 =12), gen(incomelev)
//education
encode education,gen(n_education)
encode voca,gen(n_voca)
recode n_e (32 28 33 9 41 5 14 58 34 37 8 10 11 12 59 43= 1 "No HS") (24 15 16 6 42 22 56 57 23 21 7 45 46= 2 "HS") (48 25 49 50= 3 "Some coll") (1 26 4 3 30 47 13 44 53 35 36 = 4 "I_Uni") (2 27 17 29 51 54= 5 "II_Uni") (39 20 18 38 52 55= 6 "Postuni") (31 19 40=.), gen(educlev)
recode educlev (4=5) if country=="GER" & n_voca==5

//remove case with all missing values
drop if case==312217335 & partyorder==.

//PTVs	   
	   foreach var in  PTVFUT_1 PTVFUT_2 PTVFUT_3 PTVFUT_4 PTVFUT_5 PTVFUT_6 PTVFUT_7 PTVFUT_8 PTVFUT_9 {
	 gen `var'01= .
	 replace `var'01=1 if `var'== "Always" 
	 replace `var'01=.75 if `var'=="Most of the time"  
	 replace `var'01=.5 if `var'=="About half the time"  
	 replace `var'01=.25 if `var'=="Occasionally"  
	 replace `var'01=0 if `var'=="Never" 
}
rename PTVFUT_101 ptv1 
rename PTVFUT_201 ptv2  
rename PTVFUT_301 ptv3   
rename PTVFUT_401 ptv4  
rename PTVFUT_501 ptv5  
rename PTVFUT_601 ptv6  
rename PTVFUT_701 ptv7  
rename PTVFUT_801 ptv8  

//PID
encode CLOSEPTY2, gen(party)
tab party, gen (party_)	   
recode party (42 43 44 45 23 2 =1)
recode party (4 6 9 11 18 20 25 26 28 30 34 =1)
recode party (1=.)

//gen highest-ptv party
foreach v of varlist ptv* {
	clonevar t`v' = `v'
	replace t`v' = t`v' -.005 + runiform()*.01 
}


replace tptv1 = 0 if cou=="DEN"
replace tptv3 = 0 if cou=="DEN"
replace tptv5 = 0 if cou=="DEN"
replace tptv3 = 0 if cou=="US"
replace tptv5 = 0 if cou=="CAN"
replace tptv1 = 0 if cou=="CAN"
replace tptv7 = 0 if cou=="FR"
replace tptv1 = 0 if cou=="FR"
replace tptv6 = 0 if cou=="GER"
replace tptv7 = 0 if cou=="IT"
replace tptv6 = 0 if cou=="IT"
replace tptv5 = 0 if cou=="IT"
replace tptv4 = 0 if cou=="UK"
replace tptv5 = 0 if cou=="UK"
replace tptv6 = 0 if cou=="UK"
replace tptv8 = 0 if cou=="UK"
recode tptv* (.=0)
capture drop rank*
rowranks tptv1 tptv2 tptv3 tptv4 tptv5 tptv6 tptv7 tptv8, gen (rank1 rank2 rank3 rank4 rank5 rank6 rank7 rank8 ) field


capture drop first_rank*
foreach v of varlist rank* {
	gen first_`v' = `v'==1
}

capture drop first
gen first = ""
foreach v of varlist first_rank* {
	local party = substr("`v'",11,1)
	replace first="`party'" if `v'==1
}

//gen second-highest ptv party
capture drop second_rank*
foreach v of varlist rank* {
	gen second_`v' = `v'==2
}

capture drop second
gen second = ""
foreach v of varlist second_rank* {
	local party = substr("`v'",12,1)
	replace second="`party'" if `v'==1
}


gen tptvfirst=.
replace tptvfirst=tptv1  if first=="1" 
replace tptvfirst=tptv2  if first=="2"
replace tptvfirst=tptv3 if first=="3"
replace tptvfirst=tptv4  if first=="4"
replace tptvfirst=tptv5 if first=="5"
replace tptvfirst=tptv6 if first=="6"
replace tptvfirst=tptv7 if first=="7"
replace tptvfirst=tptv8 if first=="8"
replace first= "" if tptvfirst<.2
replace first= "" if tptvfirst==.
recode tptvfirst (0/.2 = 0) (.21 / .3 =.25) (.4/.6 = .5) (.7 / .8 = .75) ( .9 / 1.1 =1), gen(ptvfirst)

gen tptvsecond=.
replace tptvsecond=tptv1  if second=="1" 
replace tptvsecond=tptv2  if second=="2"
replace tptvsecond=tptv3 if second=="3"
replace tptvsecond=tptv4  if second=="4"
replace tptvsecond=tptv5 if second=="5"
replace tptvsecond=tptv6 if second=="6"
replace tptvsecond=tptv7 if second=="7"
replace tptvsecond=tptv8 if second=="8"
replace second= "" if tptvsecond<.2
replace second= "" if tptvsecond==.
recode tptvsecond (0/.2 = 0) (.21 / .3 =.25) (.4/.6 = .5) (.7 / .8 = .75) ( .9 / 1.1 =1), gen(ptvsecond)
//gen GAP
gen ptvgap = ptvfirst - ptvsecond
 bysort cou: tab ptvgap [aw=wei]
//remove highest-PTV party if tie
 replace first ="" if ptvgap<.2

//unifying missing on immigration
replace C1_3_a = "NA" if C1_3_a=="[998]Skipped" | C1_3_a=="[999]Not Asked"
replace C1_3_b = "NA" if C1_3_b=="[998]Skipped" | C1_3_b=="[999]Not Asked"
replace C1_3_c = "NA" if C1_3_c=="[998]Skipped" | C1_3_c=="[999]Not Asked"
replace C1_3_d = "NA" if C1_3_d=="[998]Skipped" | C1_3_d=="[999]Not Asked"
replace C1_3_e = "NA" if C1_3_e=="[998]Skipped" | C1_3_e=="[999]Not Asked"
replace C1_3_f = "NA" if C1_3_f=="[998]Skipped" | C1_3_f=="[999]Not Asked"
replace C1_3_g = "NA" if C1_3_g=="[998]Skipped" | C1_3_g=="[999]Not Asked"
replace C1_3_h = "NA" if C1_3_h=="[998]Skipped" | C1_3_h=="[999]Not Asked"
replace C1_3_i = "NA" if C1_3_i=="[998]Skipped" | C1_3_i=="[999]Not Asked"
replace C3_1a = "NA" if C3_1a=="[8]Skipped" | C3_1a=="Don't Know"
replace C3_1b_1 = "NA" if C3_1b_1=="[8]Skipped" | C3_1b_1=="Don't Know"
replace C3_1b_2 = "NA" if C3_1b_2=="[8]Skipped" | C3_1b_2=="Don't Know"
replace C3_1b_3 = "NA" if C3_1b_3=="[8]Skipped" | C3_1b_3=="Don't Know"
replace C3_1b_4 = "NA" if C3_1b_4=="[8]Skipped" | C3_1b_4=="Don't Know"
replace C3_1b_5 = "NA" if C3_1b_5=="[8]Skipped" | C3_1b_5=="Don't Know"
replace C3_1b_6 = "NA" if C3_1b_6=="[8]Skipped" | C3_1b_6=="Don't Know"
replace C3_1b_7 = "NA" if C3_1b_7=="[8]Skipped" | C3_1b_7=="Don't Know"
replace C3_1b_8 = "NA" if C3_1b_8=="[8]Skipped" | C3_1b_8=="Don't Know"
//gen consitent (-1/+1) positions on immigration
encode C1_3_a, gen(C13)
recode C13 (8=.)
encode C1_3_b, gen(C13b)
recode C13b (8=.)
encode C1_3_c, gen(C13c)
recode C13c (8=.)
encode C1_3_d, gen(C13d)
recode C13d (8=.)
encode C1_3_e, gen(C13e)
recode C13e (8=.)
encode C1_3_f, gen(C13f)
recode C13f (8=.)
encode C1_3_g, gen(C13g)
recode C13g (8=.)
encode C1_3_h, gen(C13h)
recode C13h (8=.)
encode C1_3_i, gen(C13i)
recode C13i (8=.)
 replace C13 = (C13-4)/3
 replace C13b = (C13b-4)/3
  replace C13c = (C13c-4)/3
   replace C13d = (C13d-4)/3
    replace C13e = (C13e-4)/3
	 replace C13f = (C13f-4)/3
	  replace C13g = (C13g-4)/3
	   replace C13h = (C13h-4)/3
	   replace C13i = (C13i-4)/3
	   encode C3_1a , gen(C31)
 recode C31 (1=-1) (2 5 6 =.) (3=1) (4=0)
 encode C3_1b_1 , gen(C31b)
 recode C31b (1=-1) (2 5 6 7=.) (3=1) (4=0)
 encode C3_1b_2 , gen(C31c)
 recode C31c (1=-1) (2 5 6 7=.) (3=1) (4=0)
 encode C3_1b_3 , gen(C31d)
 recode C31d (1=-1) (2 5 6 7=.) (3=1) (4=0)
 encode C3_1b_4 , gen(C31e)
 recode C31e (1=-1) (2 5 6 7=.) (3=1) (4=0)
 encode C3_1b_5 , gen(C31f)
 recode C31f (1=-1) (2 5 6 7=.) (3=1) (4=0)
 encode C3_1b_6 , gen(C31g)
 recode C31g (1=-1) (2 5 6 7=.) (3=1) (4=0)
 encode C3_1b_7 , gen(C31h)
 recode C31h (1=-1) (2 5 6 7=.) (3=1) (4=0)
 encode C3_1b_8 , gen(C31i)
 recode C31i (1=-1) (2 5 6 7=.) (3=1) (4=0)
 
	   //reverse:  -1 left - +1 right    
 recode C31 C31b C31c C31d C31e C31f C31g C31h C31i  (1=-1) (-1=1)


// average party positions	   
egen mC13b = wtmean(C13b) , by(country) weight(wei)
egen mC13c = wtmean(C13c) , by(country) weight(wei)
egen mC13d = wtmean(C13d) , by(country) weight(wei)
egen mC13e = wtmean(C13e) , by(country) weight(wei)
egen mC13f = wtmean(C13f) , by(country) weight(wei)
egen mC13g = wtmean(C13g) , by(country) weight(wei)
egen mC13h = wtmean(C13h) , by(country) weight(wei)
egen mC13i = wtmean(C13i) , by(country) weight(wei)
egen mC31b = wtmean(C31b) , by(country) weight(wei)
egen mC31c = wtmean(C31c) , by(country) weight(wei)
egen mC31d = wtmean(C31d) , by(country) weight(wei)
egen mC31e = wtmean(C31e) , by(country) weight(wei)
egen mC31f = wtmean(C31f) , by(country) weight(wei)
egen mC31g = wtmean(C31g) , by(country) weight(wei)
egen mC31h = wtmean(C31h) , by(country) weight(wei)
egen mC31i = wtmean(C31i) , by(country) weight(wei)
 
// gen objective distances (from average party positions)
gen delta13_b = abs(C13 - mC13b)
gen delta13_c = abs(C13 - mC13c)
gen delta13_d = abs(C13 - mC13d)
gen delta13_e = abs(C13 - mC13e)
gen delta13_f = abs(C13 - mC13f)
gen delta13_g = abs(C13 - mC13g)
gen delta13_h = abs(C13 - mC13h)
gen delta13_i = abs(C13 - mC13i)
gen delta31_b = abs(C31 - mC31b)
gen delta31_c = abs(C31 - mC31c)
gen delta31_d = abs(C31 - mC31d)
gen delta31_e = abs(C31 - mC31e)
gen delta31_f = abs(C31 - mC31f)
gen delta31_g = abs(C31 - mC31g)
gen delta31_h = abs(C31 - mC31h)
gen delta31_i = abs(C31 - mC31i)
// gen objective distances (from individually-perceived party positions)
gen s_delta13_b = abs(C13 - C13b)
gen s_delta13_c = abs(C13 - C13c)
gen s_delta13_d = abs(C13 - C13d)
gen s_delta13_e = abs(C13 - C13e)
gen s_delta13_f = abs(C13 - C13f)
gen s_delta13_g = abs(C13 - C13g)
gen s_delta13_h = abs(C13 - C13h)
gen s_delta13_i = abs(C13 - C13i)
gen s_delta31_b = abs(C31 - C31b)
gen s_delta31_c = abs(C31 - C31c)
gen s_delta31_d = abs(C31 - C31d)
gen s_delta31_e = abs(C31 - C31e)
gen s_delta31_f = abs(C31 - C31f)
gen s_delta31_g = abs(C31 - C31g)
gen s_delta31_h = abs(C31 - C31h)
gen s_delta31_i = abs(C31 - C31i)

encode cou, gen(ncountry)
encode first, gen(nfirst)
gen nparty_ptv = 100*ncou + nfirst


//gen past-voted party dummies
encode E_1_1, gen(past1)
encode E_1_1_1, gen(past11)
gen past=. 
replace past=101 if past1==18
replace past=102 if past1==5
replace past=103 if past1==13
replace past=201 if past1==23
replace past=202 if past1==14
replace past=203 if past1==6
replace past=301 if past11==10
replace past=302 if past11==17
replace past=303 if past11==15
replace past=401 if past11==19
replace past=402 if past11==3
replace past=501 if past11==5
replace past=502 if past11==6
replace past=503 if past11==8
replace past=601 if past1==11
replace past=602 if past1==3
replace past=701 if past1==1
replace past=702 if past1==15
//gen distances from past-voted party on immigration
gen delta13_past = .
replace delta13_past = delta13_e if past==101
replace delta13_past = delta13_c if past==102
replace delta13_past = delta13_d if past==103
replace delta13_past = delta13_h if past==201
replace delta13_past = delta13_e if past==202
replace delta13_past = delta13_c if past==203
replace delta13_past = delta13_e if past==301
replace delta13_past = delta13_f if past==302
replace delta13_past = delta13_d if past==303
replace delta13_past = delta13_f if past==401
replace delta13_past = delta13_b if past==402
replace delta13_past = delta13_b if past==501
replace delta13_past = delta13_c if past==502
replace delta13_past = delta13_d if past==503
replace delta13_past = delta13_c if past==601
replace delta13_past = delta13_b if past==602
replace delta13_past = delta13_b if past==701
replace delta13_past = delta13_c if past==702
gen s_delta13_past = .
replace s_delta13_past = s_delta13_e if past==101
replace s_delta13_past = s_delta13_c if past==102
replace s_delta13_past = s_delta13_d if past==103
replace s_delta13_past = s_delta13_h if past==201
replace s_delta13_past = s_delta13_e if past==202
replace s_delta13_past = s_delta13_c if past==203
replace s_delta13_past = s_delta13_e if past==301
replace s_delta13_past = s_delta13_f if past==302
replace s_delta13_past = s_delta13_d if past==303
replace s_delta13_past = s_delta13_f if past==401
replace s_delta13_past = s_delta13_b if past==402
replace s_delta13_past = s_delta13_b if past==501
replace s_delta13_past = s_delta13_c if past==502
replace s_delta13_past = s_delta13_d if past==503
replace s_delta13_past = s_delta13_c if past==601
replace s_delta13_past = s_delta13_b if past==602
replace s_delta13_past = s_delta13_b if past==701
replace s_delta13_past = s_delta13_c if past==702
gen delta31_past = .
replace delta31_past = delta31_e if past==101
replace delta31_past = delta31_c if past==102
replace delta31_past = delta31_d if past==103
replace delta31_past = delta31_h if past==201
replace delta31_past = delta31_e if past==202
replace delta31_past = delta31_c if past==203
replace delta31_past = delta31_e if past==301
replace delta31_past = delta31_f if past==302
replace delta31_past = delta31_d if past==303
replace delta31_past = delta31_f if past==401
replace delta31_past = delta31_b if past==402
replace delta31_past = delta31_b if past==501
replace delta31_past = delta31_c if past==502
replace delta31_past = delta31_d if past==503
replace delta31_past = delta31_c if past==601
replace delta31_past = delta31_b if past==602
replace delta31_past = delta31_b if past==701
replace delta31_past = delta31_c if past==702
gen s_delta31_past = .
replace s_delta31_past = s_delta31_e if past==101
replace s_delta31_past = s_delta31_c if past==102
replace s_delta31_past = s_delta31_d if past==103
replace s_delta31_past = s_delta31_h if past==201
replace s_delta31_past = s_delta31_e if past==202
replace s_delta31_past = s_delta31_c if past==203
replace s_delta31_past = s_delta31_e if past==301
replace s_delta31_past = s_delta31_f if past==302
replace s_delta31_past = s_delta31_d if past==303
replace s_delta31_past = s_delta31_f if past==401
replace s_delta31_past = s_delta31_b if past==402
replace s_delta31_past = s_delta31_b if past==501
replace s_delta31_past = s_delta31_c if past==502
replace s_delta31_past = s_delta31_d if past==503
replace s_delta31_past = s_delta31_c if past==601
replace s_delta31_past = s_delta31_b if past==602
replace s_delta31_past = s_delta31_b if past==701
replace s_delta31_past = s_delta31_c if past==702
//gen PID with past-voted party
gen party01_past =0
replace party01_past = 1 if past==101 & party==27
replace party01_past = 1 if past==102 & party==7 & cou=="CAN"
replace party01_past = 1 if past==103 & party==21
replace party01_past = 1 if past==201 & party==35
replace party01_past = 1 if past==202 & party==22
replace party01_past = 1 if past==203 & party==8
replace party01_past = 1 if past==301 & party==37
replace party01_past = 1 if past==302 & party==41
replace party01_past = 1 if past==303 & party==24
replace party01_past = 1 if past==401 & party==33
replace party01_past = 1 if past==402 & party==5
replace party01_past = 1 if past==501 & party==29
replace party01_past = 1 if past==502 & party==13
replace party01_past = 1 if past==503 & party==12
replace party01_past = 1 if past==601 & party==16
replace party01_past = 1 if past==602 & party==7 & cou=="UK"
replace party01_past = 1 if past==701 & party==10
replace party01_past = 1 if past==702 & party==32

//gen DP: is max ptv for past-voted party?
gen normalvote =.
replace normal = 0 if past<1000
replace normal = 1 if past==101 & first=="4"
replace normal = 1 if past==102 & first=="2"
replace normal = 1 if past==103 & first=="3"
replace normal = 1 if past==201 & first=="7"
replace normal = 1 if past==202 & first=="4"
replace normal = 1 if past==203 & first=="2"
replace normal = 1 if past==301 & first=="4"
replace normal = 1 if past==302 & first=="5"
replace normal = 1 if past==303 & first=="3"
replace normal = 1 if past==401 & first=="5"
replace normal = 1 if past==402 & first=="1"
replace normal = 1 if past==501 & first=="1"
replace normal = 1 if past==502 & first=="2"
replace normal = 1 if past==503 & first=="3"
replace normal = 1 if past==601 & first=="2"
replace normal = 1 if past==602 & first=="1"
replace normal = 1 if past==701 & first=="1"
replace normal = 1 if past==702 & first=="2"

//TABLE 3
eststo clear
eststo all: logit normalvote delta13_p delta31_p i.agef sex i.educl i.income4 i.ncou party01_past
eststo s_all: logit normalvote s_delta13_p s_delta31_p i.agef sex i.educl i.income4 i.ncou party01_past
esttab using "tab_3_and_b3.rtf", replace mtitles star pr2 p wide
//TAB A4
eststo clear
eststo allml: melogit normalvote  delta13_p delta31_p i.agef sex i.educl i.income4  party01_past || ncou: delta13_p delta31_p
eststo s_allml: melogit normalvote  s_delta13_p s_delta31_p i.agef sex i.educl i.income4  party01_past || ncou: delta13_p delta31_p
esttab using "tab_b4.rtf", replace mtitles star pr2 p wide


//EXPLAINING ANTI-IMMIGRATION GAP
rename first first_ptv
//gen economic evaluations
gen sitecoret_c=.
recode sitecoret_c (.= 0) if B4_1=="Got better"
recode sitecoret_c (.= 0) if B4_1=="Gotten better"
recode sitecoret_c (.= 0) if B4_1=="Stayed about the same"
recode sitecoret_c (.= 1) if B4_1=="Gotten worse"
recode sitecoret_c (.= 1) if B4_1=="Got worse"
gen sitecoret_f=.
recode sitecoret_f (.= 0) if B4_2=="Got better"
recode sitecoret_f (.= 0) if B4_2=="Gotten better"
recode sitecoret_f (.= 0) if B4_2=="Stayed about the same"
recode sitecoret_f (.= 1) if B4_2=="Gotten worse"
recode sitecoret_f (.= 1) if B4_2=="Got worse"
 replace  sitecoret_f=.5 if  B4_2=="Stayed about the same"
 replace  sitecoret_c=.5 if  B4_1=="Stayed about the same"

//gen ideological extremism 
encode C1_1_a, gen(C11)
recode C11 (8 9 10=.)
 replace C11 = (C11-4)/3
gen extremism =abs(C11)
//gen college degree dummy
recode educl (1/3 =0 "No college") (4/6 = 1 "College degree"), gen(college)
//gen income variable from 0 to 1 with missing at .
gen income01 = (income4-1)/3
replace income01 =. if income01<0

//DISTANCES TO THE RIGHT OF  PARTIES
//objective integration
replace delta13_b = C13 - mC13b
replace delta13_c = C13 - mC13c
replace delta13_d = C13 - mC13d
replace delta13_e = C13 - mC13e
replace delta13_f = C13 - mC13f
replace delta13_g = C13 - mC13g
replace delta13_h = C13 - mC13h
replace delta13_i = C13 - mC13i
//objective level
replace delta31_b = C31 - mC31b
replace delta31_c = C31 - mC31c
replace delta31_d = C31 - mC31d
replace delta31_e = C31 - mC31e
replace delta31_f = C31 - mC31f
replace delta31_g = C31 - mC31g
replace delta31_h = C31 - mC31h
replace delta31_i = C31 - mC31i
//subjective integration
replace s_delta13_b = C13 - C13b
replace s_delta13_c = C13 - C13c
replace s_delta13_d = C13 - C13d
replace s_delta13_e = C13 - C13e
replace s_delta13_f = C13 - C13f
replace s_delta13_g = C13 - C13g
replace s_delta13_h = C13 - C13h
replace s_delta13_i = C13 - C13i
//subjective level
replace s_delta31_b = C31 - C31b
replace s_delta31_c = C31 - C31c
replace s_delta31_d = C31 - C31d
replace s_delta31_e = C31 - C31e
replace s_delta31_f = C31 - C31f
replace s_delta31_g = C31 - C31g
replace s_delta31_h = C31 - C31h
replace s_delta31_i = C31 - C31i

//DISTANCES TO THE RIGHT OF PAST VOTED PARTY
replace delta13_p = delta13_e if past==101
replace delta13_p = delta13_c if past==102
replace delta13_p = delta13_d if past==103
replace delta13_p = delta13_h if past==201
replace delta13_p = delta13_e if past==202
replace delta13_p = delta13_c if past==203
replace delta13_p = delta13_e if past==301
replace delta13_p = delta13_f if past==302
replace delta13_p = delta13_d if past==303
replace delta13_p = delta13_f if past==401
replace delta13_p = delta13_b if past==402
replace delta13_p = delta13_b if past==501
replace delta13_p = delta13_c if past==502
replace delta13_p = delta13_d if past==503
replace delta13_p = delta13_c if past==601
replace delta13_p = delta13_b if past==602
replace delta13_p = delta13_b if past==701
replace delta13_p = delta13_c if past==702
replace delta31_p = delta31_e if past==101
replace delta31_p = delta31_c if past==102
replace delta31_p = delta31_d if past==103
replace delta31_p = delta31_h if past==201
replace delta31_p = delta31_e if past==202
replace delta31_p = delta31_c if past==203
replace delta31_p = delta31_e if past==301
replace delta31_p = delta31_f if past==302
replace delta31_p = delta31_d if past==303
replace delta31_p = delta31_f if past==401
replace delta31_p = delta31_b if past==402
replace delta31_p = delta31_b if past==501
replace delta31_p = delta31_c if past==502
replace delta31_p = delta31_d if past==503
replace delta31_p = delta31_c if past==601
replace delta31_p = delta31_b if past==602
replace delta31_p = delta31_b if past==701
replace delta31_p = delta31_c if past==702

//truncate distances
 recode delta13_p (-10 /0 =0),  gen(tr_delta13_p)
 recode delta31_p (-10 /0 =0),  gen(tr_delta31_p)
 recode s_delta13_p (-10 /0 =0),  gen(tr_s_delta13_p)
 recode s_delta31_p (-10 /0 =0),  gen(tr_s_delta31_p)

 //gen government evaluation
gen gov_evaluation =.
replace gov_ =0 if B4_7=="Very bad"
replace gov_ =0.25 if B4_7=="Bad"
replace gov_ =0.25 if B4_7=="Bad "
replace gov_ =0.5 if B4_7=="Neither good nor bad "
replace gov_ =0.5 if B4_7=="Neither good nor bad"
replace gov_ =0.75 if B4_7=="Good"
replace gov_ =0.75 if B4_7=="Good "
replace gov_ =1 if B4_7=="Very good"
replace gov_ =1 if B4_7=="Very good "

//gen dummy for active workers 
gen active=.
replace active=0 if employ=="Disabled"
replace active=.5 if employ=="Full time student"
replace active=1 if employ=="Full-time"
replace active=0 if employ=="Homemaker"
replace active=0 if employ=="Laid off"
replace active=0 if employ=="Not in workforce"
replace active=0 if employ=="Not working"
replace active=.5 if employ=="Part-time"
replace active=.5 if employ=="Part-time (less than 30 hours per week)"
replace active=0 if employ=="Permanently disabled"
replace active=0 if employ=="Retired"
replace active=1 if employ=="Self-employed"
replace active=1 if employ=="Skilled / unskilled (non-salaried)"
replace active=.5 if employ=="Student"
replace active=.5 if employ=="Student/Apprentice"
replace active=0 if employ=="Taking care of home or family"
replace active=1 if employ=="Technician / salaried job (office work, education, etc.)"
replace active=0 if employ=="Temporarily laid off"
replace active=0 if employ=="Unemployed"
replace active=1 if employ=="Working full time"
replace active=1 if employ=="Working full time (30 or more hours per week)"
replace active=.5 if employ=="Working part time"
replace active=.5 if employ=="Working part time (8-29 hours a week)"
replace active=.5 if employ=="Working part time (Less than 8 hours a week)"
 
//GEN LEFT vs RIGHT mainstream party
gen left_right_past=""
replace left_right_past= "Right" if past==102
replace left_right_past= "Right" if past==202
replace left_right_past= "Right" if past==302
replace left_right_past= "Right" if past==402
replace left_right_past= "Right" if past==502
replace left_right_past= "Right" if past==602
replace left_right_past= "Right" if past==702
replace left_right_past= "Left" if past==101
replace left_right_past= "Left" if past==201
replace left_right_past= "Left" if past==301
replace left_right_past= "Left" if past==401
replace left_right_past= "Left" if past==501
replace left_right_past= "Left" if past==601
replace left_right_past= "Left" if past==701
 
 //GEN GOV vs OPPOSITION PARTY
gen ingov = 0
/* US_dem:*/    replace ingov = 1 if country=="US"  & past==701 
/* UK_con:*/    replace ingov = 1 if country=="UK"  & past==602 
/* FR_ps:*/     replace ingov = 1 if country=="FR"  & past==301
/* IT_pd:*/     replace ingov = 1 if country=="IT"  & past==501 
/* GE_cdu:*/    replace ingov = 1 if country=="GER" & past==402 
/* DE_socdem:*/ replace ingov = 1 if country=="DEN" & past==201
/* CA_con:*/    replace ingov = 1 if country=="CAN" & past==102
 rename ingov ingov_past
 replace ingov_past=. if past==103 |  past==203 | past==303 |  past==503 
 
 //gen measures of conservatism
//death penalty
 encode C2_2_a, gen(C22_01)
recode C22_01  (2=0) (5=1) (1 3 4 6 7=.)
//gay marriages
 encode C2_3_a, gen(C23_01)
recode C23_01  (2=1) (5=0) (1 3 4 6 7=.)


//ANALYSES
//tab_2
eststo clear
eststo gov_integration_OBJ: tobit tr_delta13_p age sex college income01 extrem gov_eval sitecoret_f sitecoret_c active C22_01 C23_01 i.ncou if ingov_past==1 & tr_delta31_p!=., ll(0)
eststo gov_level_OBJ: tobit tr_delta31_p age sex college income01 extrem gov_eval sitecoret_f sitecoret_c active C22_01 C23_01 i.ncou if ingov_past==1 & tr_delta13_p!=., ll(0)
eststo opp_integration_OBJ: tobit tr_delta13_p age sex college income01 extrem gov_eval sitecoret_f sitecoret_c active C22_01 C23_01 i.ncou if ingov_past==0 & tr_delta31_p!=., ll(0)
eststo opp_level_OBJ: tobit tr_delta31_p age sex college income01 extrem gov_eval sitecoret_f sitecoret_c active C22_01 C23_01 i.ncou if ingov_past==0 & tr_delta13_p!=., ll(0)
esttab using "tab_2.rtf", replace mtitles star pr2 p wide 
//tab_b2
eststo clear
eststo gov_integration_sub: tobit tr_s_delta13_p age sex college income01 extrem gov_eval sitecoret_f sitecoret_c active C22_01 C23_01 i.ncou if ingov_past==1 & tr_s_delta31_p!=., ll(0)
eststo gov_level_sub: tobit tr_s_delta31_p age sex college income01 extrem gov_eval sitecoret_f sitecoret_c active C22_01 C23_01 i.ncou if ingov_past==1 & tr_s_delta13_p!=., ll(0)
eststo opp_integration_sub: tobit tr_s_delta13_p age sex college income01 extrem gov_eval sitecoret_f sitecoret_c active C22_01 C23_01 i.ncou if ingov_past==0 & tr_s_delta31_p!=., ll(0)
eststo opp_level_sub: tobit tr_s_delta31_p age sex college income01 extrem gov_eval sitecoret_f sitecoret_c active C22_01 C23_01 i.ncou if ingov_past==0 & tr_s_delta13_p!=., ll(0)
esttab using "tab_b2.rtf", replace mtitles star pr2 p wide 

